//
//  TipPopV
//  Employee
//
//  Created by swordjoy on 2017/3/29.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class TipPopV: UIView {
  
  enum TipPopViewType {
    case finish // 完结工单
    case logout // 退出登录
    case flow
  }
  
  var confirmClosure: VoidClosure?
  
  fileprivate var backgroundV: UIView!
  fileprivate var contentV: UIView!
  
  fileprivate var titleLabel: UILabel!
  fileprivate var detailLabel: UILabel!
  fileprivate var okButton: UIButton!
  fileprivate var cancleButton: UIButton!
  
  fileprivate var separateV: UIView!
  
  fileprivate var fromFrame: CGRect!
  fileprivate var endFrame: CGRect!
  
  fileprivate var type: TipPopViewType!
  
  init(frame: CGRect, type: TipPopViewType) {
    super.init(frame: frame)
    
    backgroundColor = UIColor.clear
    
    self.type = type
    fromFrame = CGRect(x: 0, y: UIScreen.height + 220, width: UIScreen.width, height: 220)
    endFrame = CGRect(x: 0, y: UIScreen.height - 220, width: UIScreen.width, height: 220)
    
    create()
    setData()
    gesture()
    
  }

  deinit {
    SJApp.log("TipPopV")
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    
  }

}

// MARK: - interface

extension TipPopV {
  
  func show(in view: UIView){
    view.addSubview(self)
    UIView.animate(withDuration: 0.48) {
      self.contentV.frame = self.endFrame
      self.backgroundV.alpha = 1
    }
  }
  
}

// MARK: - gesture 

extension TipPopV {
  fileprivate func gesture() {
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismiss))
    backgroundV.addGestureRecognizer(tap)
  }
}

// MARK: - action

extension TipPopV {
  
  @objc fileprivate func finishAction() {
    guard let closure = confirmClosure else { return }
    closure()
    dismiss()
  }
  
  @objc fileprivate func dismiss() {
    UIView.animate(withDuration: 0.48, animations: {
      self.contentV.frame = self.fromFrame
      self.backgroundV.alpha = 0
    }) { (true) in
      self.removeFromSuperview()
    }
  }
}

// MARK: - fileprivate

extension TipPopV {
  
  fileprivate func setData() {
    

    if .finish == type {
      titleLabel.text = "您确定要完结此工单吗?"
      detailLabel.text = "在确定完结工单前请务必确认工单任务是否处理完毕。"
      okButton.setTitle("确认完结", for: .normal)
      okButton.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
    }
    else if .logout == type {
      titleLabel.text = "您确定要退出账号吗?"
      detailLabel.text = "退出账号后,您的信息将不会失去。但您将收不到关于您的任何通知信息。"
      okButton.setTitle("确认退出", for: .normal)
      okButton.setTitleColor(GlobalConfig.GrapeFruitColor, for: .normal)
    }
    else if .flow == type {
      titleLabel.text = "您确定要撤销该流程吗？"
      detailLabel.text = "撤销流程后，该流程将结束审批。"
      okButton.setTitle("确认撤销", for: .normal)
      okButton.setTitleColor(GlobalConfig.GrapeFruitColor, for: .normal)
    }
    
  }
  
  fileprivate func create() {
    backgroundV = UIView(frame: UIScreen.main.bounds)
    addSubview(backgroundV)
    backgroundV.backgroundColor = GlobalConfig.BGLightGreyColor
    backgroundV.alpha = 0
    
    contentV = UIView(frame: fromFrame)
    contentV.backgroundColor = UIColor.white
    addSubview(contentV)
    
    titleLabel = UILabel()
    contentV.addSubview(titleLabel)
    titleLabel.font = GlobalConfig.system18Font
    titleLabel.textColor = GlobalConfig.TextDeepColor
    titleLabel.snp.makeConstraints { (make) in
      make.left.equalTo(23)
      make.top.equalTo(25)
    }
    
    detailLabel = UILabel()
    contentV.addSubview(detailLabel)
    detailLabel.font = GlobalConfig.system15Font
    detailLabel.textColor = GlobalConfig.TextLightColor
    detailLabel.numberOfLines = 2
    detailLabel.snp.makeConstraints { (make) in
      make.left.equalTo(23)
      make.right.equalTo(-23)
      make.top.equalTo(titleLabel.snp.bottom).offset(10)
    }
    
    okButton = UIButton()
    contentV.addSubview(okButton)
    okButton.titleLabel?.font = GlobalConfig.system16Font
    okButton.addTarget(self, action: #selector(finishAction), for: .touchUpInside)
    okButton.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.bottom.equalTo(-78)
      make.height.equalTo(48)
    }
    
    separateV = UIView()
    contentV.addSubview(separateV)
    separateV.backgroundColor = GlobalConfig.BGWhiteColor
    separateV.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.height.equalTo(10)
      make.top.equalTo(okButton.snp.bottom)
    }
    
    cancleButton = UIButton()
    contentV.addSubview(cancleButton)
    cancleButton.setTitle("取消", for: .normal)
    cancleButton.setTitleColor(GlobalConfig.TextDeepColor, for: .normal)
    cancleButton.titleLabel?.font = GlobalConfig.system16Font
    cancleButton.addTarget(self, action: #selector(dismiss), for: .touchUpInside)
    cancleButton.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.top.equalTo(separateV.snp.bottom)
      make.bottom.equalToSuperview()
    }
    
    
  }
}
